package com.example.nutritionmate.service;

import com.example.nutritionmate.entity.User;
import com.example.nutritionmate.repository.MealNutritionRepository;
import com.example.nutritionmate.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CalorieService {
    @Autowired
    private MealNutritionRepository mealNutritionRepository;

    @Autowired
    private UserRepository userRepository;

    public Double getTotalCaloriesByUserByDate(User user, LocalDate date) {
//        LocalDateTime startOfDay = date.atStartOfDay();
//        LocalDateTime endOfDay = date.plusDays(7).atStartOfDay();
        Double calorie = mealNutritionRepository.getTotalCaloriesByUserByDate(user, date);
        if (calorie == null) {
            return 0.0;
        }
        return calorie;
    }

    public List<Double> getTotalCalorieBetween(User user, LocalDate startDate, LocalDate endDate) {
        List<Double> calories = new ArrayList<>();
        LocalDate date = startDate;
        while (!date.isAfter(endDate)) {
            calories.add(getTotalCaloriesByUserByDate(user,date));
            date = date.plusDays(1);
        }
        return calories;
    }

    public List<Double> getWeeklyCalories(User user, LocalDate endDate) {
        LocalDate startDate = endDate.minusDays(6);
        return getTotalCalorieBetween(user, startDate, endDate);
    }

}
